home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Add-Ons / MPW / MPW fixit / fixit.c next >
Encoding:
C/C++ Source or Header  |  1996-03-02  |  1.9 KB  |  99 lines  |  [TEXT/MPS ]

  1. /* File fixit.c Copyright (C) 1996 by John R. Montbriand.  All Rights Reserved. */
  2.  
  3. #include "fixit.h"
  4. #include <ToolUtils.h>
  5.  
  6. /* File fixit.c
  7.  
  8.     Copyright (C) 1996 by John Montbriand.  All Rights Reserved.
  9.     
  10.     Distribute freely in areas where the laws of copyright apply.
  11.     
  12.     Use at your own risk.
  13.     
  14.     Do not distribute modified copies.
  15.     
  16.     These various string list libraries are for free!
  17.     
  18.     See the file fixit.txt for details.
  19.     
  20. */
  21.  
  22.  
  23. /* the method for calculating the square root used here is
  24.     similar to newton's method, but originates from
  25.     the pythagorean era and involves fewer variables
  26.     and calculations.  */
  27. Fixed FixSqrt(Fixed x) {
  28.     Fixed Xn, lastXn;
  29.     long i;
  30.     
  31.         /* special cases */
  32.     if (x < 0)
  33.         return 0x80000000;
  34.     else if (x == 0)
  35.         return 0;
  36.  
  37.         /* iterate for stability, max out at 16 */
  38.     Xn = x;
  39.     lastXn = 0;
  40.     for (i = 0; i < 16; i++) {
  41.         lastXn = Xn;
  42.         Xn = FixDiv(Xn + FixDiv(x, Xn), 0x00020000);
  43.         if (lastXn == Xn) break;
  44.     }
  45.     
  46.         /* if the result is negative, we failed.. */
  47.     if (Xn < 0) return 0x80000000;
  48.     
  49.         /* done */
  50.     return Xn;
  51. }
  52.  
  53. /* anybody have any ideas about how to make this faster,
  54.     It'd be great to hear from you. */
  55. Fixed FixExp(Fixed x, Fixed a) {
  56.     Fixed y, xx, root = x, mask = 0x00008000;
  57.     
  58.         /* reorganize parameters */
  59.     if (a < 0) {
  60.         xx = FixDiv(0x00010000, x);
  61.         a = - a;
  62.     } else xx = x;
  63.     
  64.         /* calculate integral exponent part */
  65.     for (y = 0x00010000; a >= 0x00010000; a -= 0x00010000)
  66.         y = FixMul(y, xx);
  67.         
  68.         /* calculate fractional exponent part */
  69.     root = xx;
  70.     mask = 0x00008000;
  71.     while (a != 0) {
  72.         root = FixSqrt(root);
  73.         if ((a & mask) != 0) y = FixMul(y, root);
  74.         a &= (~mask);
  75.         mask >>= 1;
  76.     }
  77.  
  78.         /* done */
  79.     return y;
  80. }
  81.  
  82. Fixed FixSquare(Fixed x) {
  83.     return FixMul(x, x);
  84. }
  85.  
  86. Fixed FixCos(Fixed x) {
  87.     return Frac2Fix(FracCos(x));
  88. }
  89.  
  90. Fixed FixSin(Fixed x) {
  91.     return Frac2Fix(FracSin(x));
  92. }
  93.  
  94. Fixed FixTan(Fixed x) {
  95.     return FixDiv(FracSin(x), FracCos(x));
  96. }
  97.  
  98. /* end of file fixit.c */
  99.